package com.bestdreamer.thread_safe;

/**
 * 使用 DCL 双检查锁机制
 */
public class MyObject3 {

    private volatile static MyObject3 myObject3;

    private MyObject3(){}

    public static MyObject3 getInstance(){
        try{
            if(myObject3 != null){

            }else {
                // 模拟在创建对象之前做一些准备性的工作
                Thread.sleep(3000);
                synchronized (MyObject3.class){
                    if(myObject3 == null){
                        myObject3 = new MyObject3();
                    }
                }
            }
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        return myObject3;
    }
}
